<?php
namespace Tools\Controller;
use Common\Controller\AdminbaseController;

/**
 * 大事记后台
 */
class AdminTimelineController extends AdminbaseController{
	
	protected $cate_model;

	public function _initialize() {
		parent::_initialize();
		$this->timeline_model = D("ToolsTimeline");
	}



	// 合作单位列表
	public function index(){
	
		//条件-名称
		$content = I('content');
		if($content){
			$where['content'] = array('like',"%$content%");
		}
	
		//数量
		$count=$this->timeline_model->where($where)->count();
		
		//分页
		$page = $this->page($count, 20);
	
		$db_prefix=C('DB_PREFIX');
		$lists = $this->timeline_model
			-> field('*')
			-> where($where)
			-> order('timeline_year_month desc')
			-> limit($page->firstRow, $page->listRows)
			-> select();
	
		$this-> assign("page", $page->show('Admin'));
		$this-> assign('lists',$lists);
		$this-> display('index');
	}
	
	//填加
	public function add() {

		$this -> _modify();
	}

	//修改
	public function edit() {
		$this -> _modify();
	}


	private function _modify() {
	
		$timeline_id = I('timeline_id');

		
		if (IS_POST) {
			$result = array('status' => TRUE, 'info' => '', 'url' => '');
			
			//$this->timeline_model -> field('timeline_year_month,content,add_time');
			
			if ($this->timeline_model -> create()) {

				if (!$timeline_id) {
					$insert_id = $this->timeline_model -> add();
					if ($insert_id) {
						$result['info'] = '添加成功';
					} else {
						$result['status'] = FALSE;
						$result['info'] = '添加失败';
					}
				} else {
					if($this->timeline_model -> where(array('timeline_id' => $timeline_id)) -> save()) {
						$result['info'] = '保存成功';
					} else {
						$result['status'] = FALSE;
						$result['info'] = '保存失败';
					}
				}
			} else {
				//返回错误提示
				$result['status'] = FALSE;
				$result['info'] = $this->timeline_model -> getError();
			}
			
			if ($result['status']) {
				$this -> success($result['info'],U('Tools/AdminTimeline/index'));
			} else {
				$this -> error($result['info']);
			}
		}
	
		//修改取单个值
		$timeline = $timeline_id ? $this->timeline_model -> find($timeline_id) : array();
		$this -> assign('timeline', $timeline);

	
		//模板
		$this -> display('AdminTimeline/form');
	}


	//删除
	public function delete() {
		$timeline_id = I('timeline_id');
		$result = $this->timeline_model-> delete($timeline_id);	
		$data = array(
			'status' => $result ? true : false,
			'info' => $result ? '' : '删除失败，ID：'.$timeline_id,
			'url' => '',
		);
		$this -> ajaxReturn($data);
	}



	/**
	 * 批量操作
	 * @param string 	处理类型 order排序,delete删除
	 */
	public function indexBat($type='order') {
		if ($type == 'delete') {
			$ids = I('post.ids');
			if ($this->timeline_model -> delete(implode(',',$ids)) !== FALSE) {
				$this -> success('删除成功');
			} else {
				$this -> error('删除失败');
			}
		}
	}
	
	
}